/*
 * Copyright 2018-10-14 21:39:18 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.erp.ao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import com.lap.scd.commons.ao.AbstractAO;
import com.lap.scd.commons.constant.RespCode;
import com.lap.scd.commons.page.Page;
import com.lap.scd.commons.page.PageResult;
import com.lap.scd.commons.page.PageUtils;
import com.lap.scd.commons.result.Resp;
import com.lap.scp.erp.ao.MaterielAO;
import com.lap.scp.erp.core.MaterielDetailManager;
import com.lap.scp.erp.core.MaterielManager;
import com.lap.scp.erp.domain.MaterielDO;
import com.lap.scp.erp.domain.MaterielDetailDO;
import com.lap.scp.erp.query.MaterielQueryVO;

/**
 * <pre>
 *   默认 物料基本信息维护应用实现类，提供物料基本信息维护相关的流程应用操作、查询等。
 *
 * </pre>
 * 
 * @author 劳水生 Exp
 * @Date 2018-10-14 21:39:18
 * @since 2.0.0
 */
@Service("materielAO")
public class DefaultMaterielAO extends AbstractAO implements MaterielAO {
	/**
	 * 物料基本信息维护业务操作对象
	 */
	@Resource
	private MaterielManager materielManager;

	@Resource
	private MaterielDetailManager materielDetailManager;

	@Override
	public Resp<MaterielDO> insertMateriel(MaterielDO materielDO, BindingResult errors) {
		Resp<MaterielDO> resp = new Resp<>();
		try {
			super.validate(resp, errors, materielDO);
			if (resp.isSuccess()) {
				materielManager.insertMateriel(materielDO);
				resp.setData(materielDO);
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(materielDO, e);
		}
		return resp;
	}

	@Override
	public Resp<Integer> deleteById(Integer materielId) {
		Resp<Integer> resp = new Resp<>();
		try {
			int flag = materielManager.deleteById(materielId);
			resp.setData(flag);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(materielId, e);
		}
		return resp;
	}

	@Override
	public Resp<MaterielDO> queryById(Integer materielId) {
		Resp<MaterielDO> model = new Resp<>();
		try {
			MaterielDO materielDO = materielManager.queryById(materielId);
			model.setData(materielDO);
		} catch (Exception e) {
			model.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(materielId, e);
		}
		return model;
	}

	@Override
	public Resp<MaterielDO> queryDetailById(Integer materielId) {
		Resp<MaterielDO> model = new Resp<>();
		try {
			MaterielDO materielDO = materielManager.queryById(materielId);
			if (materielDO != null) {
				List<MaterielDetailDO> detailList = materielDetailManager.queryByMaterielId(materielId);
				materielDO.setDetailList(detailList);
			}
			model.setData(materielDO);
		} catch (Exception e) {
			model.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(materielId, e);
		}
		return model;
	}

	@Override
	public PageResult<MaterielDO> queryPage(Page page, MaterielQueryVO queryVO) {
		PageResult<MaterielDO> pr = new PageResult<>();
		try {
			List<MaterielDO> list = materielManager.queryPage(page, queryVO);

			PageUtils.copyPage(pr, page);
			pr.setData(list);
		} catch (Exception e) {
			pr.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(new Object[] { page, queryVO }, e);
		}
		return pr;
	}

	@Override
	public Resp<Integer> updateMateriel(MaterielDO materielDO, BindingResult errors) {
		Resp<Integer> resp = new Resp<>();
		try {
			super.validate(resp, errors, materielDO);
			if (resp.isSuccess()) {
				int flag = materielManager.updateMateriel(materielDO);
				resp.setData(flag);
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(materielDO, e);
		}
		return resp;
	}

}
